/**
 *用户js 
 **/

/**
 * 添加用户
 **/
function saveUser(data){
	db.transaction(function (tx) {
    	tx.executeSql("INSERT INTO USER (ID,EMAIL,IDENTITY,MOBILE,REALNAME,STATUS,USERNAME,DID,DNAME,OID,ONAME,XZQHID,XZQHNAME) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)",
    			[data.id==undefined ? '' :data.id,data.email==undefined ? '' :data.email,data.identity==undefined ? '' :data.identity,data.mobile==undefined ? '' :data.mobile,
    			 data.realname==undefined ? '' :data.realname,data.status==undefined ? '' :data.status,data.username==undefined ? '' :data.username,data.departId==undefined ? '' :data.departId,
    			 data.departName==undefined ? '' :data.departName,data.organId==undefined ? '' :data.organId,data.organName==undefined ? '' :data.organName,data.xzqhId==undefined ? '' :data.xzqhId,
    			 data.xzqhName==undefined ? '' :data.xzqhName],
       function (tx, rs) {
       },
	   function (tx, error) {
    	   message("添加用户数据失败");
	   });
    });
}
/**
 * 修改用户
 **/
function changePassword(id,password){
	db.transaction(function (tx) {
    	tx.executeSql("UPDATE USER SET NPASSWORD = ? WHERE ID = ?",
    			[password,id],
       function (tx, rs) {
    		message("修改密码成功");
       },
	   function (tx, error) {
    	    message("修改密码失败");
	   });
    });
}

/**
 * 通过ID获取用户
 **/
function findUserById(id,callback){
	db.transaction(function (tx) { 
		tx.executeSql('SELECT * FROM USER WHERE ID = ?', [id], 
		function (tx, results){ 
		    var len = results.rows.length, i;  
		    var data = {};
		    if(len == 0){
		    	return ;
		    }
		    for (i = 0; i < len; i++){
		         data.id = results.rows.item(i).ID;
		         data.username = results.rows.item(i).USERNAME;
		         data.npassword = results.rows.item(i).NPASSWORD;
		         data.oid = results.rows.item(i).OID;
		         data.oname = results.rows.item(i).ONAME;
		         data.did = results.rows.item(i).DID;
		         data.dname = results.rows.item(i).DNAME;
		         data.xzqhId = results.rows.item(i).XZQHID;
		         data.xzqhName = results.rows.item(i).XZQHNAME;
		    }
			if (typeof(callback) == 'function'){
				callback(data);
			}
			return data;
	    }, 
	    function(tx, ex) {
	    	message("获取用户信息失败");
        }); 
	});
}

/**
 * 通过名称username获取用户
 **/
function findUserByUsername(username,password,callback){
	db.transaction(function (tx) { 
		tx.executeSql("SELECT * FROM USER WHERE STATUS='激活' AND USERNAME = ? ", [username], 
		function (tx, results){ 
		    var len = results.rows.length, i;
		    $.messager.progress('close');
		    if(len > 1 || len == 0){
		    	message("获取用户信息失败");
		    	return;
		    }
		    var data = {};
		    for (i = 0; i < len; i++){
		         data.id = results.rows.item(i).ID;
		         data.username = results.rows.item(i).USERNAME;
		         data.npassword = results.rows.item(i).NPASSWORD;
		         data.oid = results.rows.item(i).OID;
		         data.oname = results.rows.item(i).ONAME;
		         data.did = results.rows.item(i).DID;
		         data.dname = results.rows.item(i).DNAME;
		         data.xzqhId = results.rows.item(i).XZQHID;
		         data.xzqhName = results.rows.item(i).XZQHNAME;
		    }
		    if(hex_md5(password) != data.npassword){
		    	message("登陆密码错误");
		    	return;
		    }
			
			if (typeof(callback) == 'function'){
				callback(data);
			}
	    }, 
	    function(tx, ex) {
	    	message("获取用户信息失败");
	    	$("#login").attr("disabled","");
        }); 
	});
}

/**
 * 删除数据
 **/
//function user(callback,data){
//	db.transaction(function (tx) { 
//		tx.executeSql('DELETE FROM USER', [],
//		function (tx, results){
//			if (typeof(callback) == 'function'){
//				callback(data);
//			}
//		}, 
//		function (tx, ex){
//			message("删除用户信息失败");
//		});  
//	});
//}

/**
 * 查询用户数据
 */
function user(callback,data){
	db.transaction(function (tx) { 
		tx.executeSql('SELECT * FROM USER WHERE ID=?', [data.id],
		function (tx, results){
				var len = results.rows.length;
			    if(len < 1){
			    	if (typeof(callback) == 'function'){
						callback(data);
					}
			    }
		});  
	});
}

/**
 * 初始化用户数据
 * @param db
 * @param callback
 **/
//function initUser(callback,data){
//	deleteUserAll(callback,data);
//}

/**
 * 从服务器上获取用户所有数据.
 **/
function getUserData(){
	$.ajax({
		type : "post",
		url : getContextPath() + "/user/findAll",
		dataType : "json",
		error : function(request) {
			message("获取用户列表数据失败");
		},
		success : function(data) {
			$.each(data, function(i, item) {
				/**
				 * 添加到本地
				 **/
				saveUser(item);
			});
			message("获取用户列表数据成功");
		}
	});
}